<?php
import( 'de.ceus-media.file.ini.Editor' );
import( 'tools.Setup.interfaces.SetupScreen' );
import( 'tools.Setup.classes.DatabaseSetupScreen' );
class DatabaseConnectionScreen extends DatabaseSetupScreen #implements SetupScreen
{
	public function handleAction( $action )
	{
		if( $action == "saveConnection" )
		{
			if( !$_POST['hostname'] )
				throw new Exception( 'Host Name is not defined.' );
			if( !$_POST['username'] )
				throw new Exception( 'User Name is not defined.' );
			if( !$_POST['database'] )
				throw new Exception( 'Database Name is not defined.' );
			$fileName	= $this->config['sources']['db_access'];
			if( !file_exists( $fileName ) )
				file_put_contents( $fileName, "" );
			$iw		= new File_INI_Editor( $fileName, TRUE );
			$iw->setProperty( 'hostname', $_POST['hostname'] );
			$iw->setProperty( 'username', $_POST['username'] );
			$iw->setProperty( 'password', $_POST['password'] );
			$iw->setProperty( 'database', $_POST['database'] );
			$iw->setProperty( 'prefix', $_POST['prefix'] );
			$action	= "testConnection";
		}
		if( $action == "testConnection" )
		{
			$this->connectDatabase( parse_ini_file( $this->config['sources']['db_access'] ) );
			return true;
		}
	}

	public function getView()
	{
		$dba	= parse_ini_file( $this->config['sources']['db_access'] );
		if( $this->config['fields'] )
		{
			foreach( $this->config['fields'] as $fieldName => $fieldDefinition )
			{
				${"label_".$fieldName}	= "<label for='".$fieldName."'>".$this->words['fields'][$fieldName]."</label>";
				if( $fieldDefinition['type'] == "input" )
				{
					${"field_".$fieldName}	= UI_HTML_Elements::Input( $fieldName, $dba[$fieldName] );
				}
			}
		}
		$content	= require_once( $this->path."templates/".$this->name.".phpt" );
		return $content;	
	}
}
?>